package org.an.proj.contacts.ui.contact.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import org.an.ku.view.MyTextView;
import org.an.proj.contacts.R;

/**
 * 创建时间：2016年10月29日14:44 <br>
 * 作者：fuchaoyang <br>
 * 描述：输入dialog
 */
public class InputDialog extends Dialog {
  @Bind(R.id.tv_title) MyTextView mTvTitle;
  @Bind(R.id.et_input) EditText mEtInput;

  private OnClickSureListener mSureListener;
  private String mTitle;
  private String mHint;
  private int mInputType = InputType.TYPE_NULL;

  public InputDialog(Context context, OnClickSureListener onSureListener) {
    super(context, R.style.dialog_base);
    this.mSureListener = onSureListener;
  }

  public InputDialog setTitle(String title) {
    mTitle = title;
    return this;
  }

  public InputDialog setHint(String hint) {
    mHint = hint;
    return this;
  }

  /**
   * {@link EditorInfo#inputType}
   */
  public InputDialog setInputType(int inputType) {
    this.mInputType = inputType;
    return this;
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_input);
    ButterKnife.bind(this);
    // 设置点击返回键消失，如果为false，返回键将无响应
    setCancelable(true);
    // 必须要设置这个，默认是match_parent
    getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT);
    getWindow().setGravity(Gravity.CENTER);
    if (mTitle != null) {
      mTvTitle.setText(mTitle);
    }
    if (mHint != null) {
      mEtInput.setHint(mHint);
    }
    if (mInputType != InputType.TYPE_NULL) {
      mEtInput.setInputType(mInputType);
    }
  }

  @Override public void onAttachedToWindow() {
    mEtInput.setText("");
  }

  @OnClick({ R.id.btn_cancel, R.id.btn_sure }) public void onClick(View view) {
    switch (view.getId()) {
      case R.id.btn_cancel:
        dismiss();
        break;
      case R.id.btn_sure:
        if (mSureListener != null) {
          mSureListener.onSure(mEtInput.getText().toString().trim());
        }
        break;
    }
  }

  public interface OnClickSureListener {
    /**
     * 点击确认按钮
     */
    void onSure(String content);
  }
}
